package org.aaron.mp3player;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.aaron.mp3player.model.Mp3Info;
import org.aaron.mp3player.utils.FileUtils;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class LocalMp3ListActivity extends ListActivity {
	private List<Mp3Info> mp3Infos;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.localmp3listlayout);
	}

	@Override
	protected void onResume() {
		super.onResume();
		FileUtils fileUtils = new FileUtils();
		mp3Infos = fileUtils.queryLocalMp3List("mp3/");
		this.setListAdapter(bundListAdapter(mp3Infos));
	}

	/**
	 * 重写ListView Item点击事件
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Mp3Info mp3Info = mp3Infos.get(position);
		Intent intent = new Intent();
		intent.putExtra("mp3info", mp3Info);
		intent.setClass(LocalMp3ListActivity.this, Mp3PlayerActivity.class);
		this.startActivity(intent);
		super.onListItemClick(l, v, position, id);
	}

	private ListAdapter bundListAdapter(List<Mp3Info> infos) {
		List<Map<String, String>> maps = new ArrayList<Map<String, String>>();
		for (Iterator<Mp3Info> iterator = infos.iterator(); iterator.hasNext();) {
			Mp3Info info = iterator.next();
			Map<String, String> map = new HashMap<String, String>();
			map.put("mp3_name", info.getMp3Name());
			map.put("mp3_size", info.getMp3Size());
			maps.add(map);
		}
		return new SimpleAdapter(LocalMp3ListActivity.this, maps, R.layout.localmp3itemlayout, new String[] { "mp3_name", "mp3_size" }, new int[] { R.id.mp3_name, R.id.mp3_size });
	}
}
